# -*- coding: utf-8 -*-
import json
import requests
from common.utils.types import Enum
from common.bankcard.model import BANKCARD_TYPE

_SEND_TEXT_API_TELE = 'https://api.telegram.org/bot701812506:AAE9waxn99hMaHjR5UZUC9bdUVp2kB8KSWQ/sendMessage'
# https://api.telegram.org/bot701812506:AAE9waxn99hMaHjR5UZUC9bdUVp2kB8KSWQ/getupdates

# 474594140 机器人id
# -211167606 审核代理告警群


def send_text_msg_tele(chat_id, chat_text):
    chat_text = chat_text.replace('[', '-').replace(']', '-').replace('_', '-')
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    post_data = {
        "chat_id": chat_id,
        "text": chat_text,
        "parse_mode": "Markdown",
    }
    print json.dumps(post_data, separators=(',', ':'))
    response = requests.post(_SEND_TEXT_API_TELE,
                             data=json.dumps(post_data, separators=(',', ':')),
                             headers=headers,
                             timeout=5)
    print response.text


TELEGRAM_GROUP = Enum({
    "HYDRA_CARD_LIMIT_TEST": (0L, u""),
    "HYDRA_CARD_LIMIT_PROD": (1L, u""),
    "HYDRA_CARD_STATUS": (2L, u""),
    "HYDRA_CARD_LIMIT_TEST_PAYMENT": (3L, u""),
    "HYDRA_CARD_LIMIT_PROD_PAYMENT": (4L, u""),
})

base_url = "https://api.telegram.org/"

test_collection = {
    "token": "698345712:AAGsezLylHj9nlOg3vZ7cqYPiM4AluDmTzM",
    "chat_id": "-1001220881308",
    "group": TELEGRAM_GROUP.HYDRA_CARD_LIMIT_TEST
}
prod_collection = {
    "token": "739073617:AAHqlOqcTDVznN-sToeWopmKnMZvdAi4qKM",
    "chat_id": "-1001161817087",
    "group": TELEGRAM_GROUP.HYDRA_CARD_LIMIT_PROD
}
test_payment = {
    "token": "698345712:AAGsezLylHj9nlOg3vZ7cqYPiM4AluDmTzM",
    "chat_id": "-254816509",
    "group": TELEGRAM_GROUP.HYDRA_CARD_LIMIT_TEST_PAYMENT
}
prod_payment = {
    "token": "739073617:AAHqlOqcTDVznN-sToeWopmKnMZvdAi4qKM",
    "chat_id": "-1001494153365",
    "group": TELEGRAM_GROUP.HYDRA_CARD_LIMIT_PROD_PAYMENT
}
card_status = {
    "token": "708890991:AAGngvnWkfWK6etsFGeVBNh9GTFcG9CMGZE",
    "chat_id": "-1001373606565",
    "group": TELEGRAM_GROUP.HYDRA_CARD_STATUS
}
chat_token = [
    test_collection, prod_collection, test_payment, prod_payment, card_status
]


def get_message_url(group):
    token = filter(lambda c: c['group'] == group, chat_token)[0]
    return "" if not token else base_url + "bot" + token[
        'token'] + "/sendmessage?chat_id=" + token[
            'chat_id'] + "&parse_mode=Markdown&text="


def send(message, host, type=BANKCARD_TYPE.RECHARGE):
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}

    group = TELEGRAM_GROUP.HYDRA_CARD_LIMIT_TEST
    if type == BANKCARD_TYPE.PAYMENT:
        group = TELEGRAM_GROUP.HYDRA_CARD_LIMIT_TEST_PAYMENT

    if host == "status.host":
        group = TELEGRAM_GROUP.HYDRA_CARD_STATUS
    elif not ("127.0.0.1" in host or "103.230.240.50" in host):
        group = TELEGRAM_GROUP.HYDRA_CARD_LIMIT_PROD
        if type == BANKCARD_TYPE.PAYMENT:
            group = TELEGRAM_GROUP.HYDRA_CARD_LIMIT_PROD_PAYMENT

    url = get_message_url(group)
    requests.get(url + message, headers=headers, timeout=5)


if __name__ == "__main__":
    # d = {
    #     "text": "\u4e0b\u5206\u901a\u9053\uff1a**4**-**\u4e1c\u839e\u5e02\u73af\u6cfd\u7535\u5b50\u79d1\u6280\u6709\u9650\u516c\u53f8** \u62a5\u9519\nPAYEE_NOT_EXIST\n\u8ba2\u5355\u53f7\uff1a%s\n",
    #     "parse_mode": "Markdown", "chat_id": -220589678}
    channel_msg = u'下分通道：**{}**-**{}** \n错误码：'.format(123, u'测试')
    error = u'哈哈哈'
    order_msg = u'\n订单号：%s\n' % 12345678
    send_text_msg_tele(-353461302, 'ttt')
